import numpy as np

A = np.array([[6, 5, 0], [5, -1, 4], [5, 1, -14], [0, 4, 3]], dtype=float)
# np.zeros_like生成跟A维度相同的矩阵，元素用0来填充
Q = np.zeros_like(A)
cnt = 0
# a是A的列向量
for a in A.T:
    u = np.copy(a)
    for i in range(0, cnt):
        # 减去待求向量在以求向量上的投影
        u -= np.dot(np.dot(Q[:, i].T, a), Q[:, i])
    # 单位化
    e = u / np.linalg.norm(u)
    Q[:, cnt] = e
    cnt += 1

R = np.dot(Q.T, A)
q, r = np.linalg.qr(A)
print(Q)
print(q)
print(R)
print(r)
